/**
 * <p><b>Title:</b> ContentWhiteController.java </p>
 * @Package com.wondertek.poms.image.controller
 */
package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.log.MethodLog;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IContentWhiteService;
import com.wondertek.poms.dao.po.ContentWhite;
import com.wondertek.poms.dao.vo.ContentWhiteVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * <p><b>ContentWhiteController Description:</b> 白名单表Controller层实现</p>
 * @author 窦再兴
 * <p><b>DATE</b> 2020-12-03 10:19:26</p>
 */
@Api(value="ContentWhiteController", description="白名单接口")
@RestController
@Scope("prototype")
@RequestMapping(value="/contentWhite")
@Validated
public class ContentWhiteController extends BaseController {
	
	@Autowired
	private IContentWhiteService contentWhiteService;
	
	@ApiOperation(value="白名单列表", notes="白名单列表")
	@GetMapping("/list")
	@MethodLog(module = "白名单列表")
	public ResultBean list(ContentWhiteVo contentWhiteVo,
			@RequestParam(defaultValue="1") int page, 
			@RequestParam(defaultValue="20") int rows,
			@RequestParam(defaultValue="updatedTime") String sidx, 
			@RequestParam(defaultValue="desc") String sord) {
		Page<ContentWhiteVo> pages = contentWhiteService
				.findPage(contentWhiteVo, page, rows, sord, sidx);
		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}

	@ApiOperation(value="白名单新增", notes="白名单新增")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "contentId", required = true),
		@ApiImplicitParam(name = "packageId", required = true),
		@ApiImplicitParam(name = "prdInfoId", required = true),
		@ApiImplicitParam(name = "remarks", required = true),
		@ApiImplicitParam(name = "createdBy", required = true),
		@ApiImplicitParam(name = "createdTime", required = true),
		@ApiImplicitParam(name = "updatedBy", required = true),
		@ApiImplicitParam(name = "updatedTime", required = true),
		@ApiImplicitParam(name = "deleted", required = true),
	})
	@PostMapping(value = "/add")
	@MethodLog(module = "白名单新增", insertDB = true, opterate = 1)
	public ResultBean add(HttpServletRequest request,@Valid @RequestBody ContentWhiteVo contentWhiteVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(contentWhiteVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		return contentWhiteService.saveOne(contentWhiteVo);
	}
	
	/**
	 * <p><b>Title:</b> isRepeat</p>
	 * <p><b>Description:</b> 判断白名单表名称是否重复</p>
	 * @author douzi
	 * @param contentWhiteVo
	 */
	private boolean isRepeat(ContentWhiteVo contentWhiteVo) {
		ContentWhite temp = new ContentWhite();
		// temp.setChannelName(contentWhiteVo.getName());
		Optional<ContentWhite> contentWhiteOpt = contentWhiteService.findOne(Example.of(temp));
		if (contentWhiteOpt.isPresent()) {
			ContentWhite contentWhite = contentWhiteOpt.get();
			if (contentWhiteVo.getId() != null && contentWhiteVo.getId() != contentWhite.getId())
				return true;
			else if (contentWhiteVo.getId() == null)
				return true;
		}
		return false;
	}
	
	@ApiOperation(value="白名单详情", notes="白名单详情")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@GetMapping(value = "/get")
	@MethodLog(module = "白名单详情")
	public ResultBean get(Long id) {
		ContentWhite contentWhite = contentWhiteService.get(id);
		return ResultBean.ok(contentWhite);
	}
	
	@ApiOperation(value="白名单变更", notes="白名单变更")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", required = true),
		@ApiImplicitParam(name = "contentId", required = true),
		@ApiImplicitParam(name = "packageId", required = true),
		@ApiImplicitParam(name = "prdInfoId", required = true),
		@ApiImplicitParam(name = "remarks", required = true),
		@ApiImplicitParam(name = "createdBy", required = true),
		@ApiImplicitParam(name = "createdTime", required = true),
		@ApiImplicitParam(name = "updatedBy", required = true),
		@ApiImplicitParam(name = "updatedTime", required = true),
		@ApiImplicitParam(name = "deleted", required = true),
	})
	@PutMapping(value = "/edit")
	@MethodLog(module = "白名单编辑", insertDB = true, opterate = 2)
	//TODO (需要删除不需要的属性列)
	public ResultBean update(HttpServletRequest request, ContentWhiteVo contentWhiteVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(contentWhiteVo, Constants.VALIDATE_UPDATE);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		if (isRepeat(contentWhiteVo))
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
		ContentWhite contentWhite = new ContentWhite();
		BeanUtils.copyProperties(contentWhiteVo, contentWhite);
		return ResultBean.ok(contentWhiteService.merge(contentWhite));
	}
	
	@ApiOperation(value="白名单删除", notes="白名单删除")
	@ApiImplicitParam(name = "ids", required = true, paramType = "query", dataType = "String")
	@DeleteMapping(value = "/del")
	@MethodLog(module = "白名单删除", insertDB = true, opterate = 3)
	public ResultBean delete(@RequestParam String ids) {
		String[] idd=ids.split(",");
		for (String id:idd) {
			contentWhiteService.deleteById(Long.parseLong(id.trim()));
		}
		return ResultBean.ok();
	}
}
